import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15

ParaCalSubItem {
    id: root
    title: qsTr("增益校准")
    width: 784
    height: 338
    property color btnColor: "#7080C0"
    property bool zeroCalOK: false
    Component.onCompleted: {
        gainCalTimer.interval = mymodbus.collectTime
        gainCalTimer.running = true;
    }
    Component.onDestruction: {
        gainCalTimer.running = false;
        gainCalTimer.destroy();
    }
    onSignalPre: {
    }
    onSignalNext: {
    }
    onSignalOK: {
        if(gainCalLoader.visible){
            return
        }
        gainCalLoader.visible=true
        if(curValue.text.length == 0)
        {
            gainCalPanelDialog.title=qsTr("请输入当前值！")
            gainCalPanelDialog.open()
            gainCalLoader.visible=false
            return;
        }
        var currentWeight =Number(curValue.text)
        if(currentWeight<0)
        {
            gainCalPanelDialog.title=qsTr("输入值不能小于0！")
            gainCalPanelDialog.open()
            gainCalLoader.visible=false
            return;
        }
        var result = mymodbus.gainCalibration(cmbGainIndex.currentIndex,currentWeight);
        gainCalLoader.visible=false

        if(result){
            //normalSettingSaveOK=true;

            paraCalView.signalSuccess()
            //normalParaSaveOKDialog.open()
//            normalParaSaveOKDialog.accepted.connect(function() {
//                                    normalParaSaveOKDialog.close()
//                                })
        }
        else
        {
            paraCalView.signalFailed()
//            normalParaSaveOKDialog.open()
//            normalParaSaveOKDialog.accepted.connect(function() {
//                                    normalParaSaveOKDialog.close();
//                                })
        }
    }
    Rectangle
    {
        width:800
        height: 120
        color:"transparent"
    GridLayout{ //root中放置一个行布局，布局中放置3个方块
        id: zeroCalibrationLayout
        anchors.centerIn: parent
        columnSpacing: 20 //间距
        rows: 3//行数
        columns: 5 //列数
        MyComboBox
        {
            id:cmbGainIndex
            Layout.preferredHeight: 30
            Layout.preferredWidth: 160
            Layout.row: 0
            Layout.column: 0
            Layout.columnSpan: 2
            font.pixelSize: 20
            //width: 160
            model:[qsTr("增益1"),qsTr("增益2"),qsTr("增益3"),qsTr("增益4")]
        }
//        Text
//        {
//            Layout.row: 1
//            Layout.column: 0
//            text:qsTr("砝码")
//            color:"white"
//            font.pixelSize: 20

//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//        }
//        Button
//        {
//            font.pixelSize: 20
//            Layout.row: 1
//            Layout.column: 1
//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//            id:hasWeight
//            text: qsTr("有")
//            background: Rectangle{
//                id:recHasWeight
//                border.width: 0
//                color: "lightgreen"
//                    }
//            onClicked: {
//                recHasWeight.color = "lightgreen"
//                recNoWeight.color = "darkgray"
//                btnCurValue.visible = false
//                recCurValue.visible = false
//                miniKeyBoard.visible = false;
//            }
//            palette.buttonText: "white"
//        }
//        Button
//        {
//            font.pixelSize: 20
//            Layout.row: 1
//            Layout.column: 2
//            Layout.preferredWidth: 80
//            Layout.preferredHeight: 30
//            id:noWeight
//            text: qsTr("无")
//            background: Rectangle{
//                id:recNoWeight
//                border.width: 0
//                color: "darkGray"
//                    }
//            onClicked: {
//                recHasWeight.color="darkgray"
//                recNoWeight.color = "lightgreen"
//                btnCurValue.visible = true
//                recCurValue.visible = true
//                miniKeyBoard.visible = true;
//            }
//            palette.buttonText: "white"
//        }
        Button
        {
            id: btnCurValue
            Layout.row:1
            Layout.column:0
            Layout.preferredWidth: 80
            Layout.preferredHeight: 30
            text: qsTr("当前值")
            font.pixelSize: 18
            background: Rectangle{
                color: btnColor
                    }
            onClicked: {
                //miniKeyBoard.lineValue = "";
                //miniKeyBoard.textInput=oldPwd;

            }
        }

        Rectangle {
            id: recCurValue
            //width:300
            //height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:1
            Layout.column:1
            Layout.columnSpan: 2
            Layout.preferredWidth: 180
            Layout.preferredHeight: 30
            // 绘制一条边框
            Shape {
                anchors.fill: parent
                ShapePath {
                    strokeWidth: 3
                    strokeColor: "white"
                    strokeStyle: ShapePath.SolidLine
                    startX: 0
                    startY: recCurValue.height
                    PathLine {
                        x: recCurValue.width
                        y: recCurValue.height
                    }
                }
            }
            TextInput{
                id:curValue
                anchors.fill: parent
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 25
                horizontalAlignment: Text.AlignHCenter
                readOnly: true
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                       }
                   }

            }
        }

        // Button
        // {
        //     id: btnCurrent
        //     Layout.row:1
        //     Layout.column:2
        //     Layout.preferredWidth: 80
        //     Layout.preferredHeight: 35
        //     text: qsTr("电流")
        //     font.pixelSize: 18
        //     background: Rectangle{
        //         color: btnColor
        //             }
        //     onClicked: {
        //         //miniKeyBoard.lineValue = "";
        //         //miniKeyBoard.textInput=oldPwd;
        //     }
        // }

        Rectangle {
            id: recCurrentValue
            width:180
            height:30
            // x: 218
            // y: 7
            color: "transparent"
            border.width: 0
            border.color: "white"
            Layout.row:0
            Layout.column:2
            Layout.columnSpan: 2
            Layout.preferredWidth: 180
            Layout.preferredHeight: 30
            // 绘制一条边框
//            Shape {
//                anchors.fill: parent
//                ShapePath {
//                    strokeWidth: 3
//                    strokeColor: "white"
//                    strokeStyle: ShapePath.SolidLine
//                    startX: 0
//                    startY: recCurrentValue.height
//                    PathLine {
//                        x: recCurrentValue.width
//                        y: recCurrentValue.height
//                    }
//                }
//            }
            TextInput{
                id:currentValue
                anchors.fill: parent
                //text:qsTr("电压:")+mymodbus.current0.toString()+"mV"
                //echoMode: TextInput.Password
                color:"white"
                font.pixelSize: 20
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignBottom
                readOnly: true
                MouseArea {
                       anchors.fill: parent
                       onClicked: {
                           // miniKeyBoard.lineValue = "";
                           // miniKeyBoard.textInput=oldPwd;
                       }
                   }

            }
        }

        Rectangle {
                id:stableLight
                Layout.row:0
                Layout.column:4
                Layout.preferredWidth: 30
                Layout.preferredHeight: 30
                z:100
                //anchors.horizontalCenter: parent.horizontalCenter
                //y: 125;                  // 位于父窗体的y位置，以左上角为起点，缺省为0
                //width: 30;            // 宽度，此时Rectangle控件在Window控件内，属于Window的孩子，可以使用parent继承
                //height: 30;           // 高度，此时Rectangle控件在Window控件内，属于Window的孩子，可以使用parent
                color: "transparent";         // 颜色，缺省为白色
                opacity: 1;          // 透明度，缺省为1
                radius: 15;           // 圆角，通过圆角来画出一个圆形窗体出来
                //clip: true;            // 截断，在Rectangle控件内的子控件，超出他本身大小后会被截断
            }
    }
    }
    Text {
        id:gainCalTip
        x:0
        y:150
        text:qsTr("请先放置砝码")+"\n"+qsTr("砝码重量大于量程1/3")+"\n"+qsTr("输入砝码重量值")+"\n"+qsTr("待指示灯变绿")+"\n"+qsTr("点击“确认”")+"\n"+qsTr("完成增益标定")
        //wrapMode: Text.Wrap
        color:"white"
        font.pixelSize: 20
        horizontalAlignment: Text.AlignLeft
        verticalAlignment: Text.AlignBottom
    }
    MiniKeyBoard
    {
        id: miniKeyBoard
        width:800
        //x: 34
        y: 126
        isNumber: true
        textInput: curValue
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: gainCalLoader
        visible:false
    }
    Dialog {
        anchors.centerIn: parent
        closePolicy: "CloseOnEscape"
        modal:true
        id: gainCalPanelDialog
        standardButtons: Dialog.Ok
//        onAccepted: {
//            zeroCalPanelDialog.close()
//            if(zeroCalOK){
//                bottomBar.selected=0
//                //myLoader.setsource="StatusView.qml"
//                myLoader.visible=false;
//                statusView.visible=true;
//            }
//        }
        z:100
    }
    Timer{
        id:gainCalTimer
        //interval: mymodbus.collectTime
        running: false
        repeat: true
        onTriggered: {
            currentValue.text = qsTr("电压:")+mymodbus.voltage+"uV"
            stableLight.color = mymodbus.stable===1? "green":"red";
        }

    }
}
